﻿using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 保存根据付款银行账号查询对应客户信息，赋值给客户字段
    /// </summary>
    /// <remarks>
    /// 功能：获取付款银行账号查询对应客户
    /// 创建人：wzx
    /// 创建日期：2024.5.21
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    [Description("待认领收款清单保存插件")]
    [HotUpdate]
    public class PendingCollectionListService : AbstractOperationServicePlugIn
    {
        //OnPreparePropertys 数据加载前，确保需要的属性被加载
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            //付款方银行账号
            e.FieldKeys.Add("F_RDLV_PayerBankAccount");
        }
        /// <summary>
        /// 事务结束后事件
        /// </summary>
        /// <param name="e"></param>
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);

            //读取全部的单据,for循环,转换成DynamicObject类型
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string FID = entity["ID"].ToString();
                string FBillNo = entity["BillNo"].ToString();
                if (entity["F_RDLV_PayerBankAccount"] != null && entity["F_RDLV_PayerBankAccount"].ToString() != "")
                {

                    string PayerBankAccount = entity["F_RDLV_PayerBankAccount"].ToString();//付款方银行账号


                    string SQL = @"/*dialect*/ SELECT FCUSTID from T_BD_CUSTOMER where F_RDLV_CRMId in( SELECT [客户id] FROM [CRM].[ecology].[dbo].[crm_yhzh]  where yxzh = '" + PayerBankAccount + "')";


                    DataTable dt = DBServiceHelper.ExecuteDataSet(this.Context, SQL).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        string SQLUpdate = @"/*dialect*/update RDLV_t_PendingCollection set F_QLTK_Cust='" + Convert.ToString(dt.Rows[0]["FCUSTID"]) + "' where FID='" + FID + "' ";
                        DBServiceHelper.Execute(this.Context, SQLUpdate);
                    }

                }


            }
        }


    }
}
